package com.wx.activitys.drp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.wx.activitys.drp.JsonResult;
import com.wx.activitys.drp.model.BDictionary;
import com.wx.activitys.drp.model.BRole;
import com.wx.activitys.drp.model.BUserInfo;
import com.wx.activitys.drp.service.BDictionaryService;
import com.wx.activitys.drp.service.BUserInfoService;
import com.wx.activitys.drp.service.ShareService;
import com.wx.activitys.drp.service.UserInfoService;
import com.wx.activitys.drp.service.impl.RoleServiceImpl;

@Controller
public class MemberCenterController {
	public static final String ROOT_NAME = "drp";

	@Autowired
	private RoleServiceImpl roleService;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private ShareService shareService;

	@Autowired
	private BDictionaryService bDictionaryService;
	@Autowired
	private BUserInfoService bUserInfoService;
	

	/**
	 * 分销统计
	 */
	@RequestMapping(value = "statistics.html", method = RequestMethod.GET)
	public String statistics(Model model, HttpServletRequest request) {
		GetUserInfoResponse userInfo = ((GetUserInfoResponse) request.getSession().getAttribute("userInfo"));
		if (userInfo != null) {
		  return ROOT_NAME + "/statistics";
		}else{
		  return "error/error";
		}
	}
	
	@RequestMapping(value = "editProfit.html", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(Model model, HttpServletRequest request,@RequestParam Double value,@RequestParam String platfromId) {
		GetUserInfoResponse userInfo = ((GetUserInfoResponse) request.getSession().getAttribute("userInfo"));
		if (userInfo != null) {
			Map<String,Object> map = new HashMap<>();
			map.put("value", value);
			map.put("openid", userInfo.getOpenid());
			map.put("platfromid", platfromId);
			int flag=shareService.update(map);
			if(flag>0){
				return JsonResult.success("修改成功",flag);
			}
			return JsonResult.fail("修改失败", flag);
		}else{
		  return JsonResult.fail("修改失败", "");
		}
	}
	

	/**
	 * 经销商信息
	 */
	@RequestMapping(value = "dealerInfor.html", method = RequestMethod.GET)
	public String dealerInfor(Model model, HttpServletRequest request) {
		//注释内容显示各个产品分润，暂时注掉
		/*GetUserInfoResponse userInfo = ((GetUserInfoResponse) request.getSession().getAttribute("userInfo"));
		if (userInfo != null) {
			String openid =userInfo.getOpenid();
		    //String openid = "oCdHJw6bk1jzMU_WWcDe90NTZERg";
		    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		    Map<String,Object> map = new HashMap<>();
		    List<BShare> shareList =null;
		    map.put("openid",openid);
		    map.put("month", sdf.format(new Date()));
			BUserBusiness userBusiness = userBusinessService.findByMap(map);
			model.addAttribute("userBusiness", userBusiness);// 当月的业务量
			BUserInfo  bu =  userInfoService.findByOpenid(openid);
			BRole role = roleService.findByRoleKey(bu.getRoleKey());
			model.addAttribute("roleName", role.getName());// 当前用户经销商等级
			model.addAttribute("treamRole",bu.getTreamRole());
			if(!bu.getTreamRole().equals("TOP")){
				shareList= shareService.findByOpenid(bu.getpId());
				if(!bu.getTreamRole().equals("LOW")){
				  List<BShare> ownshareList= shareService.findByOpenid(openid);
				  model.addAttribute("ownshareList", ownshareList);
				}
				model.addAttribute("shareList", shareList);
			}else{
				map.put("roleKey", bu.getRoleKey());
				List<BProfit> shareLists= bProfitService.linkFindMessageByMap(map);
				model.addAttribute("shareLists", shareLists);
			} 
			return ROOT_NAME + "/dealerInfor";
		} else {
			return "error/error";
		}*/
		GetUserInfoResponse userInfo = ((GetUserInfoResponse) request.getSession().getAttribute("userInfo"));
		if (userInfo != null) {
			String openid =userInfo.getOpenid();
		    //String openid = "oCdHJw6ejjKPH64faM9lVlVlvwvA";
			BUserInfo  bu =  userInfoService.findByOpenid(openid);
			BRole role = roleService.findByRoleKey(bu.getRoleKey());
			BDictionary entity = new BDictionary();
			entity.setType(bu.getRoleKey());
			BDictionary dictionary=bDictionaryService.selectEntity(entity);
			model.addAttribute("roleName", role.getName());
			Map<String,Object> map = new HashMap<>();
			map.put("pId", openid);
			List<BUserInfo> bUserInfos=userInfoService.selectByMap(map);
			BUserInfo bu1 = new BUserInfo();
			bu1.setpId(openid);
			int count=bUserInfoService.selectCount(bu1);
			int flag=0;
			if(Integer.valueOf(dictionary.getValue())>count){
				flag=1;
			}
			model.addAttribute("flag", flag);
			model.addAttribute("bUserInfos", bUserInfos);
			return ROOT_NAME + "/dealerInfor2";
		} else {
			return "error/error";
		}
		

	}
	
	
   /* @RequestMapping(value = "dealerInfor1.html", method = RequestMethod.GET)
	public String dealerInfo1r(Model model, HttpServletRequest request) {
		shareServiceImpl.initData2();
		return null;
		}*/

}
